﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreData;
using EntityData;
using Helper;
using Services.BusinessRules;
namespace Services
{

    public partial class SubjectService
    {
       readonly ISubjectEntity entity;

       public SubjectService()
       {
           this.entity = new SubjectEntity();

       }

       #region Main Method

       /// <summary>
       /// Save 
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public long Save(object _model, ref string mess)
       {
           try
           {
               if (Validate((Subject)_model, ref mess))
               {
                   var id = long.Parse(_model.GetType().GetProperty("ID").GetValue(_model, null).ToString());
                   if (id == 0)
                   {
                       return entity.Save((CoreData.Subject)_model, Table.Subject.ToString());
                   }
                   else
                   {
                       return entity.Update((CoreData.Subject)_model, Table.Subject.ToString());
                   }
               }
               else
               {
                   return -1;
               }
           }
           catch { return -1; }
       }

       /// <summary>
       /// Get by ID
       /// </summary>
       /// <param name="_id"></param>
       /// <returns></returns>
       public CoreData.Subject GetByID(long _id)
       {
           try
           {
               return entity.GetByCusTomSQL(String.Format(SQLCommand.GetSubjects," Subject.ID="+_id)).ToList()[0];
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Subject> GetList()
       {
           try
           {
               return entity.GetByCusTomSQL(String.Format(SQLCommand.GetSubjects, " Subject.IsDeleted='false' and 1=1")).ToList();
           }
           catch { return null; }

       }

       /// <summary>
       /// Get List with Linq
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Subject> GetListByLINQ(Func<CoreData.Subject, Boolean> _where)
       {
           try
           {
               return entity.GetMany(_where, Table.Subject.ToString()).ToList();
           }
           catch { return null; }
       }
       /// <summary>
       /// Get One with Linq
       /// </summary>
       /// <returns></returns>
       public CoreData.Subject GetOneByLINQ(Func<CoreData.Subject, Boolean> _where)
       {
           try
           {
               return entity.Get(_where, Table.Subject.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List with search string
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Subject> GetList(string _searchstring)
       {
           //search theo tieu chi nao do         
           try
           {
               return entity.GetByCusTomSQL(String.Format(SQLCommand.GetSubjects, "Subject.IsDeleted = 'false' and " + _searchstring)).ToList();
           }
           catch { return null; }
       }


       /// <summary>
       /// Delete
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public bool Delete(object _model)
       {
           try
           {
               entity.Delete((CoreData.Subject)_model, Table.Subject.ToString());
               return true;
           }
           catch { return false; }
       }

       /// <summary>
       /// Delete a object by hidden
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public bool UpdateIsDelete(object _model)
       {
           try
           {
               var obj = (CoreData.Subject)_model;
               obj.IsDeleted = true;
               entity.Update(obj, Table.Subject.ToString());
               return true;
           }
           catch { return false; }
       }

       #endregion



        #region Other Method

       bool Validate(Subject obj, ref string mess)
       {

           new ValidateRequired("Mã môn học", "Code", obj.Code);
           new ValidateRequired("Tên môn khóa", "Name", obj.Name);
           new ValidateExist("Mã môn học", "Code", "Code", obj, GetList().Cast<object>().ToList());
           new ValidateExist("Tên môn học", "Name", "Name", obj, GetList().Cast<object>().ToList());
           new ValidateID("Ngành học", "Branch", obj.BranchID);
           mess = BusinessObject.Validate();
           BusinessObject.Clear();
           return mess == null ? true : false;
       }

        #endregion 
    
    }
         
}
